/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.repair.entity.WxFaultCause;
import org.springblade.modules.repair.entity.WxFaultType;
import org.springblade.modules.repair.service.IWxFaultTypeService;
import org.springblade.modules.repair.vo.WxFaultCauseVO;
import org.springblade.modules.repair.mapper.WxFaultCauseMapper;
import org.springblade.modules.repair.service.IWxFaultCauseService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.repair.vo.WxFaultTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 维修原因表 服务实现类
 *
 * @author ysq
 * @since 2025-06-30
 */
@Service
public class WxFaultCauseServiceImpl extends BaseServiceImpl<WxFaultCauseMapper, WxFaultCause> implements IWxFaultCauseService {

	@Autowired
	private IWxFaultTypeService wxFaultTypeService;

	@Override
	public List<WxFaultCauseVO> getTreeList() {
		List<WxFaultCause> list = this.list(Wrappers.<WxFaultCause>lambdaQuery().eq(WxFaultCause::getStatus, 1).orderByDesc(WxFaultCause::getSort));
		List<WxFaultCause> oneList = list.stream().filter(o -> o.getParentId() == 0).collect(Collectors.toList());
		Map<Long, List<WxFaultCause>> towMap = list.stream().filter(o -> o.getParentId() != 0 && o.getParentId() != null).collect(Collectors.groupingBy(WxFaultCause::getParentId));
		List<WxFaultCauseVO> resultList = new ArrayList<>(100);
		Map<Long, List<WxFaultType>> listMap = wxFaultTypeService.list().stream().collect(Collectors.groupingBy(WxFaultType::getId));
		for (WxFaultCause wxFaultCause : oneList) {
			WxFaultCauseVO wxFaultCauseVO = new WxFaultCauseVO();
			wxFaultCauseVO.setId(wxFaultCause.getId().toString());
			wxFaultCauseVO.setName(wxFaultCause.getFaultName());
			wxFaultCauseVO.setSort(wxFaultCause.getSort());
			wxFaultCauseVO.setStatus(wxFaultCause.getStatus());
			wxFaultCauseVO.setIsOther(wxFaultCause.getIsOther());
			//封装责任归属
			String faultTypes = wxFaultCause.getFaultTypes();
			List<WxFaultTypeVO> wxFaultTypeList = new ArrayList<>(16);
			if (StrUtil.isNotEmpty(faultTypes)) {
				String[] split = faultTypes.split(",");
				for (String typeId : split) {
					WxFaultTypeVO wxFaultTypeVO = new WxFaultTypeVO();
					if (listMap.get(Convert.toLong(typeId)) != null && listMap.get(Convert.toLong(typeId)).size() > 0) {
						WxFaultType wxFaultType = listMap.get(Convert.toLong(typeId)).get(0);
						wxFaultTypeVO.setId(wxFaultType.getId().toString());
						wxFaultTypeVO.setName(wxFaultType.getName());
						wxFaultTypeVO.setSort(wxFaultType.getSort());
						wxFaultTypeVO.setIsOther(wxFaultType.getIsOther());
						wxFaultTypeVO.setStatus(wxFaultType.getStatus());
						wxFaultTypeList.add(wxFaultTypeVO);
					}
				}
				wxFaultCauseVO.setWxFaultTypeList(wxFaultTypeList);
			}

			//分装二级原因
			List<WxFaultCauseVO> childList = new ArrayList<>(16);
			List<WxFaultCause> causeList = towMap.get(wxFaultCause.getId());
			if (causeList != null && causeList.size() > 0) {
				for (WxFaultCause wxFaultCause1 : causeList) {
					WxFaultCauseVO towWxFaultCauseVO = new WxFaultCauseVO();
					towWxFaultCauseVO.setId(wxFaultCause1.getId().toString());
					towWxFaultCauseVO.setName(wxFaultCause1.getFaultName());
					towWxFaultCauseVO.setSort(wxFaultCause1.getSort());
					towWxFaultCauseVO.setIsOther(wxFaultCause1.getIsOther());
					towWxFaultCauseVO.setStatus(wxFaultCause1.getStatus());
					String towFaultTypes = wxFaultCause1.getFaultTypes();
					List<WxFaultTypeVO> towWxFaultTypeList = new ArrayList<>(16);
					if (StrUtil.isNotEmpty(towFaultTypes)) {
						String[] split = towFaultTypes.split(",");
						for (String typeId : split) {
							WxFaultTypeVO wxFaultTypeVO = new WxFaultTypeVO();
							WxFaultType wxFaultType = listMap.get(Convert.toLong(typeId)).get(0);
							wxFaultTypeVO.setId(wxFaultType.getId().toString());
							wxFaultTypeVO.setName(wxFaultType.getName());
							wxFaultTypeVO.setSort(wxFaultType.getSort());
							wxFaultTypeVO.setIsOther(wxFaultType.getIsOther());
							wxFaultTypeVO.setStatus(wxFaultType.getStatus());
							towWxFaultTypeList.add(wxFaultTypeVO);
						}
					}
					towWxFaultCauseVO.setWxFaultTypeList(towWxFaultTypeList);
					childList.add(towWxFaultCauseVO);
				}
				wxFaultCauseVO.setChildList(childList);
			}
			resultList.add(wxFaultCauseVO);
		}
		return resultList;
	}

	@Override
	public List<WxFaultTypeVO> getTypeList() {

		List<WxFaultTypeVO> resultList = new ArrayList<>(16);
		List<WxFaultType> list = wxFaultTypeService.list(Wrappers.<WxFaultType>lambdaQuery().eq(WxFaultType::getStatus, 1).orderByDesc(WxFaultType::getSort));
		for (WxFaultType wxFaultType : list) {
			WxFaultTypeVO wxFaultTypeVO = new WxFaultTypeVO();
			wxFaultTypeVO.setId(wxFaultType.getId().toString());
			wxFaultTypeVO.setName(wxFaultType.getName());
			wxFaultTypeVO.setIsOther(wxFaultType.getIsOther());
			wxFaultTypeVO.setStatus(wxFaultType.getStatus());
			resultList.add(wxFaultTypeVO);
		}
		return resultList;
	}
}
